<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>按钮点击示例 - 背景色切换</title>
    <style>
        /* 页面全局样式 */
        body {
            font-family: '微软雅黑', sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            color: #333;
        }

        /* 容器样式 */
        .container {
            text-align: center;
            background-color: #fff;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 500px;
        }

        h1 {
            font-size: 2.5rem;
            color: #333;
            margin-bottom: 20px;
        }

        p {
            font-size: 1.2rem;
            color: #555;
            margin-bottom: 30px;
        }

        /* 按钮组样式 */
        .button-group {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            justify-content: center;
        }

        /* 按钮样式 */
        button.btn {
            background-color: #4CAF50;
            color: white;
            font-size: 1.2rem;
            border: none;
            padding: 15px 30px;
            cursor: pointer;
            border-radius: 8px;
            width: 150px;
            transition: background-color 0.3s ease, transform 0.2s ease;
            outline: none;
        }

        button.btn:hover {
            background-color: #45a049;
            transform: scale(1.05);
        }

        button.btn:active {
            background-color: #388E3C;
            transform: scale(1);
        }

        /* 提示语 */
        .hint {
            font-size: 1rem;
            margin-top: 20px;
            color: #777;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>按钮点击示例</h1>
        <p>点击任意按钮，按钮的背景色会随机变化。</p>
        <div class="button-group">
            <button class="btn">按钮 1</button>
            <button class="btn">按钮 2</button>
            <button class="btn">按钮 3</button>
            <button class="btn">按钮 4</button>
            <button class="btn">按钮 5</button>
        </div>
        <p class="hint">提示：每次点击按钮都会随机改变背景颜色！</p>
    </div>

    <script>
        // 将类数组对象（如 NodeList 或 HTMLCollection）转换为数组的函数
        const toArray = (value = '') =>
            Array.from ? Array.from(value) : Array.prototype.slice.call(value);

        // 获取所有的按钮元素
        const buttons = document.querySelectorAll('.btn');

        // 使用 toArray 函数将 NodeList 转换为数组
        const buttonArray = toArray(buttons);

        // 给每个按钮绑定点击事件
        buttonArray.forEach((button) => {
            button.addEventListener('click', () => {
                // 随机生成一个颜色
                const randomColor = getRandomColor();
                // 改变按钮的背景色
                button.style.backgroundColor = randomColor;
            });
        });

        // 生成随机颜色的函数
        function getRandomColor() {
            const letters = '0123456789ABCDEF';
            let color = '#';
            for (let i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 16)];
            }
            return color;
        }
    </script>
</body>

</html>